package com.facedetect;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.json.JsonRootBean;
import com.json.Location;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final String ACCESS_TOKEN = "24.a60bf86325617002b8568ed2bd57d3e7.2592000.1563496352.282335-16561675";
    private Button btnGetToken;
    private Button btnDetect;
    private Location location;
    private Handler handler;
    private int GET_FACE_LOCATION = 1001;
    private MyImageView ivImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnGetToken = findViewById(R.id.btn_get_token);
        btnDetect = findViewById(R.id.btn_detect);
        ivImg = findViewById(R.id.iv_img);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_FACE_LOCATION){
                    ivImg.showFace(location);
                    return true;
                }
                return false;
            }
        });

        btnGetToken.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String token = AuthService.getAuth();
                        Log.e(TAG, "token: " + token);
                    }
                }).start();
            }
        });

        btnDetect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String imgPath = "/mnt/shell/emulated/0/Images/wyz.png";
                        byte[] wyzArr = Base64Util.image2byte(imgPath);
                        String wyzBase64 = Base64Util.encode(wyzArr);
                        Log.e(TAG, "wyzBase64: " + wyzBase64);
                        location = FaceDetect.detect(wyzBase64);
                        handler.sendEmptyMessage(GET_FACE_LOCATION);
                    }
                }).start();
            }
        });
    }
}
